package com.softer.chengxin.report.dao.provider;

import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.score.entity.ScoreCompositeProvince;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * @author Gong Zhiyang
 * @date 2020/1/18
 */

public class ReportPrintDaoProvider {

    public String getReportList(Map<String, ScoreCompositeProvince> map) {
        StringBuilder sql = new StringBuilder(6);
        ScoreCompositeProvince compositeProvince = map.get("s");
        String area = compositeProvince.getArea();
        sql.append("SELECT p.id,p.basic_score as basicScore,p.excellent_score as excellentScore,p.negative_score as negativeScore,");
        sql.append("p.company_id as companyId,p.`level` as level, p.`year` as year,p.manage_score as manageScore,c.`name` as qyName");
        sql.append(" FROM `score_composite_province` p LEFT JOIN company_base_info c ON p.company_id =c.id");
        sql.append(" where 1=1");
        if (StringUtils.isNotBlank(area)) {
            sql.append(" and (CASE c.community WHEN 0 THEN c.street in (").append(area).append(") ELSE c.community in (").append(area).append(") end)");
        }
        if (StringUtils.isNotBlank(compositeProvince.getQyName())) {
            sql.append(" AND INSTR(c.name,#{s.qyName})>0 ");
        }
        if (StringUtils.isNotBlank(compositeProvince.getLevel())) {
            if (Common.CANCEL.equals(compositeProvince.getLevel())) {
                sql.append(" and c.level_state=0");
            } else {
                sql.append(" and r.level=#{s.level}");
            }
        }
        if (compositeProvince.getDistrict() != null) {
            sql.append(" and c.district=#{s.district}");
        }
        if (StringUtils.isNotBlank(compositeProvince.getRegisterType())) {
            sql.append(" and c.register_type=#{s.registerType}");
        }
        return sql.toString();
    }
}
